//
//  BaseTabBarController.m
//  UI12_UITabBarController
//
//  Created by yutao on 15/10/9.
//  Copyright © 2015年 yutao. All rights reserved.
//

#import "BaseTabBarController.h"
#import "CustomButton.h"

@interface BaseTabBarController ()


@property(nonatomic,retain)CustomButton *previousBtn;

@end

#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@implementation BaseTabBarController

//自定义设置tabbar是否隐藏
- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController
{
    //先删掉自己自定义的
    [self.tabBarImageView removeFromSuperview];

    if (hidden) {

        [self.tabBar setHidden:NO];
        [self.tabBarImageView setFrame:self.tabBar.bounds];
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.tabBarImageView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }

    }else
    {   
        [self.tabBar setHidden:YES];
        [self.tabBarImageView setFrame:self.tabBar.frame];

        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.tabBarImageView];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
    
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    self.tabBarImageView.backgroundColor = BARCOLOR;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [super viewDidLoad];
    self.tabBarImageView = [[UIImageView alloc] init];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"0"]) {
        self.tabBarImageView.backgroundColor = [UIColor colorWithWhite:0.251 alpha:1.000];
    }else{
        self.tabBarImageView.backgroundColor = [UIColor colorWithRed:0.309 green:0.573 blue:0.728 alpha:1.000];
    }
    
    self.tabBarImageView.userInteractionEnabled = YES;
    [_tabBarImageView release];


    [self isHiddenTabBar:NO withViewController:nil];

    // 下面的方法是调用自定义的生成按钮的方法
    [self creatButtonWithNormalName:@"iconfont-tuijian.png"andSelectName:@"iconfont-tuijiandangqian.png"andTitle:@"推荐"andIndex:0];

    [self creatButtonWithNormalName:@"iconfont-mudedi.png"andSelectName:@"iconfont-mudedi-2.png"andTitle:@"目的地"andIndex:1];

    [self creatButtonWithNormalName:@"iconfont-shequ.png"andSelectName:@"iconfont-shequ-2.png"andTitle:@"社区"andIndex:2];

    [self creatButtonWithNormalName:@"iconfont-wode.png"andSelectName:@"iconfont-wode-2.png"andTitle:@"我的"andIndex:3];


    //默认选中第一个按钮
    CustomButton *btn = self.tabBarImageView.subviews[0];
    [self changeViewController:btn]; //自定义的控件中得按钮被点击了调用的方法，默认进入界面就选中第一个按钮

}


#pragma mark 创建按钮通用方法
- ( void )creatButtonWithNormalName:( NSString *)normal andSelectName:( NSString *)selected andTitle:( NSString *)title andIndex:( int )index

{

    CustomButton *button = [CustomButton buttonWithType:UIButtonTypeCustom ];

    button. tag = index;

    CGFloat buttonW = self.tabBarImageView.frame.size.width / 4;

    CGFloat buttonH = self.tabBarImageView.frame.size.height;
    button. frame = CGRectMake ( WIDTH/4 *index, 4 , buttonW, buttonH);

    [button setImage :[UIImage imageNamed:normal] forState:UIControlStateNormal];

    [button setImage :[UIImage imageNamed:selected] forState : UIControlStateDisabled];//设置按钮无效时的图片样式

    [button setTitle:title forState:UIControlStateNormal];

    [button addTarget:self action:@selector (changeViewController:) forControlEvents : UIControlEventTouchDown];

    button.imageView.contentMode = UIViewContentModeCenter ; // 让图片在按钮内居中

    button.titleLabel.textAlignment = NSTextAlignmentCenter ; // 让标题在按钮内居中

    button.titleLabel.font = [ UIFont systemFontOfSize:12]; // 设置标题的字体大小
    button.titleLabel.textColor = [UIColor blackColor];

    [self.tabBarImageView addSubview:button];

}

#pragma mark 按钮被点击时调用

- ( void )changeViewController:( CustomButton *)sender

{

    //*****关键,设置选中的控制器
    self.selectedIndex = sender.tag ; //切换不同控制器的界面
    
    sender.enabled = NO ;//让选中的按钮变为不可用,也就是限制点击一次
    
    //如果当前点击的按钮不是保存的按钮.那么将按钮设置为可用状态.
    //也就是两次点击的不是同一个按钮
    if ( self.previousBtn != sender) {
        
        self.previousBtn.enabled = YES ;
        
    }
    //记录当前点击的按钮
    self.previousBtn = sender;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
